package com.trainingcloud.ctrl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.trainingcloud.mapper.DailyMapper;
import com.trainingcloud.pojo.Daily;
import com.trainingcloud.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 学生日报表 前端控制器
 * </p>
 *
 * @author 纸鹤
 * @since 2022-10-11
 */
@RestController
public class DailyController {

    @Autowired
    private DailyMapper dailyMapper;

    @RequestMapping("/getDailys")
    public ResultUtil getDailys(Daily daily) {
        ResultUtil ok = ResultUtil.ok();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        QueryWrapper<Daily> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("relation_class_id", daily.getRelationClassId());
        queryWrapper.eq("create_time", format.format(daily.getCreateTime()));
        List<Daily> dailies = dailyMapper.selectList(queryWrapper);
        ok.setData(dailies);
        return ok;
    }

    @RequestMapping("/getStudentDailys")
    public ResultUtil getStudentDailys(Daily daily) {
        ResultUtil ok = ResultUtil.ok();
        QueryWrapper<Daily> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("relation_class_id", daily.getRelationClassId());
        queryWrapper.eq("relation_student_id", daily.getRelationStudentId());
        List<Daily> dailies = dailyMapper.selectList(queryWrapper);
        ok.setData(dailies);
        return ok;
    }

    @RequestMapping("/updateDailyComment")
    public ResultUtil updateDailyComment(Daily daily) {
        ResultUtil ok = ResultUtil.ok();
        dailyMapper.updateById(daily);
        return ok;
    }

    @RequestMapping("/addDaily")
    public ResultUtil addDaily(Daily daily) {
        ResultUtil ok = ResultUtil.ok();
        daily.setCreateTime(new Date());
        dailyMapper.insert(daily);
        return ok;

    }
}

